package com.dkm.ec.pay.domain;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.api.helper.IConfigHelper;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.ec.pay.persistence.dao.PayChargeDao;
import com.dkm.ec.pay.persistence.entity.PayChargePo;
import com.dkm.ec.pay.repository.PayChargeRepository;
import com.dkm.ec.sale.domain.SaleEntity;
import com.dkm.ec.sale.persistence.entity.SaleEntityPo;
import com.dkm.ec.sale.repository.SaleEntityRepository;
import com.dkm.ec.so.domain.SoEntity;
import com.dkm.ec.so.domain.SoItem;
import com.dkm.ec.so.persistence.dao.SoEntityDao;
import com.dkm.ec.so.persistence.dao.SoItemDao;
import com.dkm.ec.so.persistence.entity.SoEntityPo;
import com.dkm.ec.so.persistence.entity.SoItemPo;
import com.dkm.ec.so.repository.SoEntityRepository;
import com.dkm.ec.so.repository.SoItemRepository;
import com.pingplusplus.Pingpp;
import com.pingplusplus.model.Charge;

/**
 * 对象功能:t_pay_charge 领域对象实体
 * 开发公司:dkm.com
 * 开发人员:ZHH
 * 创建时间:2017-05-23 11:03:44
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class PayCharge extends AbstractDomain<String, PayChargePo>{
	@Resource
	 private PayChargeDao payChargeDao = null;
	 @Resource
	 private PayChargeRepository payChargeRepository;
	 @Resource
     IConfigHelper configHelper;
	 @Resource
	 private SoItemDao soItemDao;
	 @Resource
	 private SoEntityRepository soEntityRepository;
	 @Resource
	 private SoItemRepository soItemRepository;
	 @Resource
	 private SoEntityDao soEntityDao;
	 

	protected void init(){
		payChargeDao = SpringHelper.getBean(PayChargeDao.class);
		setDao(payChargeDao);
	}

	public Charge createCharge(Map<String, Object> chargeParams,String saleEntityId) throws Exception{
		Pingpp.apiKey = configHelper.getParamValue("pay.ping.appKey");
	   	Pingpp.privateKeyPath= configHelper.getParamValue("pay.ping.privateKeyPath");
		String appId = configHelper.getParamValue("pay.ping.appId");
		BigDecimal totalPrice = new BigDecimal("0");
		
		
		String soNo = "";
    	SoEntity soEntity = soEntityRepository.load(saleEntityId);	
    	SoEntityPo soEntityPo = soEntity.getData();
    	totalPrice = soEntityPo.getTotal();
    	soNo = soEntityPo.getSoNo();
	
		
		String body =  soNo;
		String subject = soNo;
   	    BigDecimal totalBigDecimal = new BigDecimal("100").multiply(totalPrice); 
   	    chargeParams.put("amount", totalBigDecimal.intValue());//订单总金额, 人民币单位：分（如订单总金额为 1 元，此处请填 100）
     	chargeParams.put("currency", "cny");
   	
        chargeParams.put("subject",subject);
   	    chargeParams.put("body", body);
   	    chargeParams.put("order_no", soNo);// 推荐使用 8-20 位，要求数字或字母，不允许其他字符
       Map<String, String> app = new HashMap<String, String>();
       app.put("id",appId);
       chargeParams.put("app", app);

	    Charge charge  = Charge.create(chargeParams);
	
		PayChargePo chargePo = new PayChargePo();
		BeanUtils.copyProperties(chargePo, charge);
		chargePo.setId(null);
		chargePo.setSoEntityId(saleEntityId);
		chargePo.setStatus("create");
		chargePo.setSoNo(soNo);
		chargePo.setChargeId(charge.getId());
		
		PayCharge payCharge = payChargeRepository.newInstance(chargePo);
		payCharge.save();
	
	return charge;
}

	public void updateCharge(Charge pingppObject) {
	   
		Map<String, Object> params = new HashMap<>();
		params.put("whereSql", "charge_id_='"+pingppObject.getId()+"'");
		List<PayChargePo> payChargePos = payChargeRepository.findAll(params);
		if(payChargePos.size() > 0){
			PayChargePo payChargePo = payChargePos.get(0);
			payChargePo.setStatus("success");
			PayCharge payCharge = payChargeRepository.newInstance(payChargePo);
			payCharge.save();
			
		/*	String type = payChargePo.getType();
			if("soEntity".equals(type)){*/
				Map<String, Object> params1 = new HashMap<>();
				params1.put("soEntityId", payChargePo.getSoEntityId());
				params1.put("status", "awaiting_ship");
				soItemDao.updateByKey("updateBySoentityId", params1);
				
				Map<String, Object> params2 = new HashMap<>();
				params2.put("soEntityId", payChargePo.getSoEntityId());
				params2.put("status", "awaiting_ship");
				soEntityDao.updateByKey("updateById", params2);
				
			/*}else{
				Map<String, Object> params1 = new HashMap<>();
				params1.put("soEntityId", payChargePo.getSoEntityId());
				params1.put("status", "awaiting_ship");
				soItemDao.updateByKey("updateById", params1);
			}*/
		}
	}
}
